package com.juyu.miliao.api.controller;

import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.service.LineTimeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.Instant;

@RestController
@RequestMapping("/lineTime")
public class LineTimeController {

	private final Logger logger = LoggerFactory.getLogger("hostLineTimeLogin");
	@Resource
	private StringRedisTemplate stringRedisTemplate;
	@Resource
	private UserInfoDao userInfoDao;
	@Resource
	LineTimeService lineTimeService;

	/**
	 * 前端维护主播登录状态，每5分钟心跳一次
	 * @param userId
	 */
	@GetMapping("/host/login/{userId}")
	public void synHostLineTime(@PathVariable("userId") Integer userId){
		if(userId!=null && userId !=0){
			User user = userInfoDao.getUserInfoById(Long.parseLong(userId+""));
			if(user!=null && user.getIsHost()==1){
				logger.info("前端同步主播登录时间::::::userId="+userId);
				//eventType 值为2，表示是登录事件的消息
				lineTimeService.countLineTime("2",userId+"", Instant.now().toEpochMilli()+"",user);
				//登出操作队列
				stringRedisTemplate.opsForZSet().remove("hostLineTimeSignOut",userId+"");
				stringRedisTemplate.opsForZSet().add("hostLineTimeSignOut",userId+"",Instant.now().toEpochMilli());
			}
		}
	}

}